<!--
@name:dialogPointConfig
@author:Zhenglei
@date:15:00
@description:dialogPointConfig
@update:2022/8/1615:00
-->
<template>
  <el-dialog title="配置"
             :visible.sync="visible"
             :before-close="modalClose"
             :append-to-body="true"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="60%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>

      <el-form-item label="年龄段:" class="is-required">
        <el-radio-group v-model="chooseData.isAllAge">
          <el-radio :label="1">不限</el-radio>
          <el-radio :label="0">指定年龄段</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="chooseData.isAllAge===0" prop="ageCondition">
        <el-select v-model="subData.ageCondition"
                   class="w-300"
                   clearable
                   multiple
                   placeholder="请选择年龄段">
          <el-option v-for="item in ageDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别：" prop="genderMode">
        <el-select v-model="subData.genderMode"
                   class="w-300"
                   clearable
                   placeholder="请选择性别">
          <el-option v-for="item in sexDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="bodyRiskMode">
        <el-select v-model="subData.bodyRiskMode"
                   class="w-300"
                   clearable
                   placeholder="请选择状态">
          <el-option v-for="item in statusDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="病症:" class="is-required">
        <el-radio-group v-model="chooseData.isAllDisease">
          <el-radio :label="1">不限</el-radio>
          <el-radio :label="0">指定病种</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item v-if="chooseData.isAllDisease===0">
        <el-tree
          :data="diseaseDataSource"
          show-checkbox
          node-key="id"
          :default-checked-keys="defaultCheckedKeys"
          :props="defaultProps"
          @check="handleCheckChange">
        </el-tree>
      </el-form-item>


    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>
</template>

<script>
export default {
  name: "dialogPointConfig",
  props: {
    pointConfigData: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      chooseData: {
        isAllAge: 1,
        isAllDisease: 1,
      },
      subData: {
        indicationId: '',
        constantDetectId: '',
        ageCondition: '',
        genderMode: '',
        bodyRiskMode: '',
        themeIds: [],
      },
      ageDataSource: [
        {
          id: '1',
          name: '婴儿期，0-6岁',
          disabled: false
        }, {
          id: '2',
          name: '少年儿童，7-12岁',
          disabled: false
        }, {
          id: '3',
          name: '青少年，13-17岁',
          disabled: false
        }, {
          id: '4',
          name: '青年，18-39岁',
          disabled: false
        }, {
          id: '5',
          name: '中年，40-64岁',
          disabled: false
        }, {
          id: '6',
          name: '老年，≥65岁',
          disabled: false
        },
      ],
      sexDataSource: [
        {
          id: 0,
          name: '不限'
        }, {
          id: 2,
          name: '女'
        }, {
          id: 1,
          name: '男'
        }
      ],
      statusDataSource: [
        {
          id: 0,
          name: '不限'
        }, {
          id: 1,
          name: '中风险以下'
        }, {
          id: 2,
          name: '中风险及以上'
        }
      ],
      diseaseDataSource: [],
      defaultProps: {
        label: "themeName",
        children: "children"
      },
      defaultCheckedKeys: [],
      formModelRules: {
        ageCondition: [{required: true, message: '请选择年龄段', trigger: 'blur'},],
        genderMode: [{required: true, message: '请选择性别', trigger: 'blur'},],
        bodyRiskMode: [{required: true, message: '请选择状态', trigger: 'blur'},],
      },
    }
  },
  created() {
    this.getDiscLifeIndicationsDetail()
    this.getTreeList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          if (this.chooseData.isAllDisease === 0 && this.subData.themeIds.length === 0) {
            this.$message.error('请选择病症')
          } else if (this.chooseData.isAllDisease === 1) {
            this.subData.themeIds = []
          }
          if (this.chooseData.isAllAge === 1) {
            this.subData.ageCondition = []
            this.subData.ageCondition.push(0)
          }
          let mydata = JSON.parse(JSON.stringify(this.subData))
          mydata.ageCondition = mydata.ageCondition.toString()
          this.post(this.api.healthMap.updateDolConstantIndications, mydata).then(res => {
            if (res.data.code === '200') {
              this.$message.success('保存成功')
              this.modalClose()
              this.$parent.getDiscLifeIndications()
            }
          })
        }
      })
    },
    //查询慢病病种
    getTreeList() {
      this.post(this.api.promote.getTreeList, {
        pageIndex: 0,
        pageSize: 100,
        searchNoChronic: 1
      }).then(res => {
        if (res.data.code === '200') {
          this.diseaseDataSource = res.data.object;
        }
      })
    },
    //查询详情
    getDiscLifeIndicationsDetail() {
      this.post(this.api.healthMap.getDolConstantIndicationsDetail, {
        indicationId: this.pointConfigData.indicationId,
        constantDetectId: this.pointConfigData.themeId,
      }).then(res => {
        if (res.data.code === '200') {
          if (!res.data.object.indicationId) {
            this.$message.warning('请配置！')
            this.subData.indicationId = this.pointConfigData.indicationId
            this.subData.constantDetectId = this.pointConfigData.themeId
            return
          }
          this.subData = Object.assign(this.subData, res.data.object);
          if (this.subData.ageCondition) {
            if (this.subData.ageCondition === '0') {
              //不限年龄
              this.chooseData.isAllAge = 1
            } else {
              this.chooseData.isAllAge = 0
              this.subData.ageCondition = res.data.object.ageCondition.split(',')
            }
            if (this.subData.themeIds.length === 0) {
              this.chooseData.isAllDisease = 1
            } else {
              this.chooseData.isAllDisease = 0
              this.defaultCheckedKeys = res.data.object.themeIds
            }
          }

        }
      })
    },
    handleCheckChange(obj, selectObj) {
      let checkedNodes = selectObj.checkedKeys;
      checkedNodes = checkedNodes.filter(item => item)

      this.subData.themeIds = checkedNodes
    },
  },
}
</script>

<style scoped>

</style>
